﻿// -----------------------------------------------------------------------
// <copyright file="Validator.cs" company="123">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Macds.Core.Validation
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public abstract class Validator<T>
    {
        private T obj;
        protected abstract IEnumerable<IValidationUnit> GetValidations(T obj);

        public T Instance
        {
            get
            {
                return this.obj;
            }
            set
            {
                this.obj = value;
            }
        }

        protected void ValidateItems()
        {
            foreach (var validation in GetValidations(obj))
            {
                if (validation.Validate() == false)
                {
                    throw validation.InternalException;
                }
            }
        }

        public T Validate()
        {
            ValidateItems();
            return this.Instance;
        }
    }
}
